package com.example.studentsRoom.controller;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.studentsRoom.entity.Building;
import com.example.studentsRoom.entity.Dormitory;
import com.example.studentsRoom.form.SearchForm;
import com.example.studentsRoom.service.impl.BuildingService;
import com.example.studentsRoom.util.ResultUtil;
import com.example.studentsRoom.vo.BuildingVo;
import com.example.studentsRoom.vo.PageVo;
import com.example.studentsRoom.vo.ResultVo;
import com.example.studentsRoom.vo.StudentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author admin
 * @since 2022-07-19
 */
@RestController
@RequestMapping("/building")
public class BuildingController {

    @Autowired
    private BuildingService buildingService;

    /**
     * 获取楼宇信息
     * @return
     */
    @GetMapping("/buildList")
    public ResultVo buildList(){
        List<Building> list = buildingService.list();
        if (list!=null){
            return ResultUtil.success(list);
        }else{
            return ResultUtil.failed();
        }
    }

    /**
     * 添加楼宇
     * @param building
     * @return
     */
    @GetMapping("/save")
    public ResultVo save(Building building){
        boolean save = buildingService.save(building);
        if (save){
            return ResultUtil.success(null);
        }else return ResultUtil.failed();
    }

    /**
     * 分页查询楼宇
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/list/{page}/{size}")
    public ResultVo list(
           @PathVariable("page") Integer page,
           @PathVariable("size") Integer size
    ){
        PageVo pageVo = buildingService.listPage(page, size);
        if (pageVo!=null){
            return ResultUtil.success(pageVo);
        }else return ResultUtil.failed();
    }

    /**
     * 模糊查询+分页
     * @param searchForm
     * @return
     */
    @GetMapping("/search")
    public ResultVo search(SearchForm searchForm){
        PageVo pageVo=buildingService.search(searchForm);
        return ResultUtil.success(pageVo);
    }

    /**
     * 根据id来找信息
     * @param id
     * @return
     */
    @GetMapping("/findById/{id}")
    public ResultVo findById(@PathVariable("id") Integer id){
        Building build = buildingService.getById(id);

        return ResultUtil.success(build);
    }

    /**
     * 更新楼宇信息
     * @param building
     * @return
     */
    @GetMapping("/upData")
    public ResultVo upData(Building building){
        UpdateWrapper<Building> wrapper=new UpdateWrapper<>();
        wrapper.eq("b_id",building.getBId());

        boolean update = buildingService.update(building, wrapper);
        if (update){
            return ResultUtil.success(null);
        }else return ResultUtil.failed();
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @DeleteMapping("/deleteById/{id}")
    public ResultVo deleteById(@PathVariable("id") Integer id){
        boolean b = buildingService.removeById(id);
        if (b){
            return ResultUtil.success(null);
        }else{
            return ResultUtil.failed();
        }
    }
}

